package com.moon.game.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.moon.game.command.Command;
import com.moon.game.config.ConfigUtil;
import com.moon.game.constants.GlobalConstants;
import com.moon.game.exception.GException;

public class CommandSet {
	public static CommandSet inst = null;
	private Map<String, Command> cmdSet = new HashMap<String,Command >();

	private CommandSet(){
	}

	public static void init() throws GException{
		if(inst == null ){
			inst = new CommandSet();
		}
	}

	public void loadCommand() throws GException {
		Properties prop = ConfigUtil.loadProperties(GlobalConstants.COMMAND_FIRE);
		for(Object key : prop.keySet()){
			String clazz = prop.getProperty((String)key);
			if(clazz == null) {
				throw new GException("cmd_load_fatal:class "+(String)key+" is null");
			}
			try {
				String[] k = ((String)key).split(",");
				Command cmd = (Command)Class.forName(clazz).getConstructor().newInstance();
				for (String name : k) {
					cmdSet.put(name, cmd);
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new GException("cmd_load_fatal:class "+(String)key+" ");
			}
		}
	}

	public Command getCommand(String cmdName){
		return cmdSet.get(cmdName);
	}

	public String printCommandSet(){
		return cmdSet.toString();
	}

	/**
	 * @return the cmdSet
	 */
	public Map<String, Command> getCmdSet() {
		return cmdSet;
	}
}
